package com.eedi.framework.notify.dal.mapper;

import com.eedi.framework.common.enums.UserTypeEnum;
import com.eedi.framework.common.enums.YesOrNoEnum;
import com.eedi.framework.common.pojo.PageResult;
import com.eedi.framework.mybatis.core.mapper.BaseMapperX;
import com.eedi.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.eedi.framework.mybatis.core.query.QueryWrapperX;
import com.eedi.framework.notify.controller.sys.vo.message.SysNotifyMessageMyPageReq;
import com.eedi.framework.notify.controller.sys.vo.message.SysNotifyMessagePageReq;
import com.eedi.framework.notify.dal.dataobject.SysNotifyMessageDO;
import org.apache.ibatis.annotations.Mapper;

import java.time.LocalDateTime;
import java.util.Collection;
import java.util.List;

@Mapper
public interface NotifyMessageMapper extends BaseMapperX<SysNotifyMessageDO> {

    default PageResult<SysNotifyMessageDO> selectPage(SysNotifyMessagePageReq reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<SysNotifyMessageDO>()
                .eqIfPresent(SysNotifyMessageDO::getSysUserId, reqVO.getSysUserId())
                .eqIfPresent(SysNotifyMessageDO::getSysUserType, reqVO.getSysUserType())
                .likeIfPresent(SysNotifyMessageDO::getSysNoticeCode, reqVO.getSysNotifyCode())
                .eqIfPresent(SysNotifyMessageDO::getSysNoticeType, reqVO.getSysNotifyType())
                .betweenIfPresent(SysNotifyMessageDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(SysNotifyMessageDO::getSysNotifyMessageId));
    }

    default PageResult<SysNotifyMessageDO> selectPage(SysNotifyMessageMyPageReq reqVO, String userId, UserTypeEnum userType) {
        return selectPage(reqVO, new LambdaQueryWrapperX<SysNotifyMessageDO>()
                .eqIfPresent(SysNotifyMessageDO::getSysNoticeReadStatus, reqVO.getSysNoticeReadStatus())
                .betweenIfPresent(SysNotifyMessageDO::getCreateTime, reqVO.getCreateTime())
                .eq(SysNotifyMessageDO::getSysUserId, userId)
                .eq(SysNotifyMessageDO::getSysUserType, userType)
                .orderByDesc(SysNotifyMessageDO::getSysNotifyMessageId));
    }

    default int updateListRead(Collection<Long> ids, String userId, UserTypeEnum userType) {
        return update(new SysNotifyMessageDO().setSysNoticeReadStatus(YesOrNoEnum.YES).setSysNoticeReadTime(LocalDateTime.now()),
                new LambdaQueryWrapperX<SysNotifyMessageDO>()
                        .in(SysNotifyMessageDO::getSysNotifyMessageId, ids)
                        .eq(SysNotifyMessageDO::getSysUserId, userId)
                        .eq(SysNotifyMessageDO::getSysUserType, userType)
                        .eq(SysNotifyMessageDO::getSysNoticeReadStatus, false));
    }

    default int updateListRead(String userId, UserTypeEnum userType) {
        return update(new SysNotifyMessageDO().setSysNoticeReadStatus(YesOrNoEnum.YES).setSysNoticeReadTime(LocalDateTime.now()),
                new LambdaQueryWrapperX<SysNotifyMessageDO>()
                        .eq(SysNotifyMessageDO::getSysUserId, userId)
                        .eq(SysNotifyMessageDO::getSysUserType, userType)
                        .eq(SysNotifyMessageDO::getSysNoticeReadStatus, false));
    }

    default List<SysNotifyMessageDO> selectUnreadListByUserIdAndUserType(String userId, UserTypeEnum userType, Integer size) {
        // 由于要使用 limitN 语句，所以只能用 QueryWrapperX
        return selectList(new QueryWrapperX<SysNotifyMessageDO>()
                .eq("user_id", userId)
                .eq("user_type", userType)
                .eq("read_status", false)
                .orderByDesc("id").limitN(size));
    }

    default Long selectUnreadCountByUserIdAndUserType(String userId, UserTypeEnum userType) {
        return selectCount(new LambdaQueryWrapperX<SysNotifyMessageDO>()
                .eq(SysNotifyMessageDO::getSysNoticeReadStatus, false)
                .eq(SysNotifyMessageDO::getSysUserId, userId)
                .eq(SysNotifyMessageDO::getSysUserType, userType));
    }

}
